package cn.zwxky.android.user.HomePage;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.ClipboardManager;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.utils.SN;

import java.util.ArrayList;
import java.util.List;

import cn.zwxky.android.user.R;
import cn.zwxky.android.user.Utils.LittleUtils;
import cn.zwxky.android.user.Utils.PopWindowManager;

import static com.baidu.location.h.j.p;


/**
 * 定位记录
 * Created by Administrator on 2017/3/21 0021.
 */
@ContentView(R.layout.aty_location)
public class LocationActivity extends LsfbActivity {



    /**
     * RecyclerView的布局管理器
     */
    private RecyclerView.LayoutManager mlayoutManager;

    /**
     * 适配器
     */
    private LocationAdapter adapter;

    /**
     * 用于存放获取到的数据
     */
    private List<LocationBean.ListBean> datas;

    private List<LocationBean.ListBean> pointList;

    /**
     * list列表
     */
    @ViewInject(R.id.dish_list)
    private RecyclerView dish_list;

    /**
     * PopWindow管理类
     */
    private PopWindowManager replay;

    private LocationDBHelper dbHelper;
    private Cursor mCursor;
    private int location_id=1;

    @Override
    public void initView() {
        LittleUtils.setsimpletitlebar(this, "经纬度");
        dbHelper = new LocationDBHelper(this);
        mCursor=dbHelper.select();

        datas = new ArrayList<>();
        pointList=new ArrayList<>();
        adapter = new LocationAdapter(this, datas, R.layout.item_location);
        mlayoutManager = new LinearLayoutManager(this);//布局模式为listview方式
        dish_list.setLayoutManager(mlayoutManager);
        dish_list.setAdapter(adapter);



    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {
        getData();
    }

    public void getData(){
        location_id=1;
        pointList.clear();
        LocationBean.ListBean point = null;
        mCursor.moveToFirst();
        while (mCursor.moveToNext()) {
            point = new LocationBean.ListBean();
            point.setId(location_id);
            point.setLongitude(mCursor.getString(mCursor.getColumnIndex("longitude")));
            point.setLatitude(mCursor.getString(mCursor.getColumnIndex("latitude")));
            point.setElevation(mCursor.getString(mCursor.getColumnIndex("elevation")));
            point.setTime(mCursor.getString(mCursor.getColumnIndex("time")));
            pointList.add(point);
            location_id++;
        }
        datas.clear();
        datas.addAll(pointList);
        adapter.notifyDataSetChanged();
    }


    @EventAnnotation
    public void LocationClickEvent(final LocationClickEvent event) {
        replay = new PopWindowManager(LocationActivity.this, R.layout.pop_location) {
            @Override
            public void onViewLister(View contentView, final PopWindowManager manager) {

                final TextView pop_exit_fuzhi = (TextView) contentView.findViewById(R.id.pop_exit_fuzhi);
                final TextView pop_exit_delete = (TextView) contentView.findViewById(R.id.pop_exit_delete);
                final TextView pop_location_text1 = (TextView) contentView.findViewById(R.id.pop_location_text1);
                final TextView pop_location_text2 = (TextView) contentView.findViewById(R.id.pop_location_text2);
                final RelativeLayout RelativeLayout = (RelativeLayout) contentView.findViewById(R.id.RelativeLayout);
                //北纬34°43′20″   东经34°43′20″
                pop_location_text1.setText("北纬" + event.getData().getLongitude() + "    东经" + event.getData().getLatitude());
                pop_location_text2.setText("海拔" + event.getData().getElevation() + "米");

                //复制
                pop_exit_fuzhi.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                        cm.setText("北纬" + event.getData().getLongitude() + "   东经" + event.getData().getLatitude() + "  海拔" + event.getData().getElevation() + "米");
                        SN.SnackShow(dish_list, "复制成功");
                        manager.hideWindow();
                    }
                });

                //删除
                pop_exit_delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mCursor.moveToPosition(event.getData().getId());
                        //删除
                        dbHelper.delete(mCursor.getInt(0));
                        mCursor.requery();
                        getData();

                        manager.hideWindow();
                    }
                });

                RelativeLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        manager.hideWindow();
                    }
                });

            }
        };
        replay.showWindowsCenter(LocationActivity.this.getWindow().getDecorView());

    }

}
